package com.imodia.checkoo.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hsq.commons.util.StringUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.SqlParam;
import com.hsq.commons.util.db.dbUtil.UpdateFieldBean;
import com.hsq.commons.util.html.BaseUrl;
import com.imodia.checkoo.bean.CmsBaseSourceBean;
import com.imodia.checkoo.service.impl.CmsBaseSourceServiceImpl;

@Controller
@RequestMapping("/hfmanager/base_source/list.do")
public class CmsBaseSourceController {

	
	private final Log log = LogFactory.getLog(CmsBaseSourceController.class);
	
	private final String baseUrl = "base_source";
	
	private final String url_list = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list";
	private final String url_redirect = "/hfmanager/" +baseUrl + "/list.do?action=list";
	private final String url_edit = "/hfmanager/" + baseUrl + "/"+baseUrl+"_edit";

	@Autowired
	private CmsBaseSourceServiceImpl cmsBaseSourceService;
	
	@RequestMapping(params = "action=delete")
	public ModelAndView delete(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		cmsBaseSourceService.delete(id);
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect), null);
	}

	@RequestMapping(params = "action=save")
	public ModelAndView edit(@ModelAttribute("userMessage")CmsBaseSourceBean obj, HttpServletRequest request) {
		int x = 0;
		if(obj.getId()!=null && obj.getId()>0){
			x = cmsBaseSourceService.update(obj);
		}else{
			x = cmsBaseSourceService.insert(obj);
			obj.setId(x);
		}
		if(x>0)x=1;
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect+"&x="+x+"&id="+obj.getId()), null);
	}
	
	@RequestMapping(params = "action=edit")
	public ModelAndView edit_in(HttpServletRequest request,CmsBaseSourceBean obj) {
		int id = StringUtil.requestInt(request,"id");
		Map<String,Object> m = new HashMap<String,Object>();

		m.put("info", cmsBaseSourceService.get(id));
	    return new ModelAndView(url_edit, m);
	}
	
	@RequestMapping(params = "action=list")
	public ModelAndView list(HttpServletRequest request, CmsBaseSourceBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		PageResult<CmsBaseSourceBean> pageResult = cmsBaseSourceService.page(new SqlParam(),page);
		model.put("page", pageResult);
		return new ModelAndView(url_list, model);
	}
	
	
	
	@RequestMapping(params = "action=updateFieldValue")
	public ModelAndView updateFieldValue(UpdateFieldBean obj,BindingResult result){
		int message = 1;
		if(obj.getTableName()==null || obj.getTableName().trim().length()<=0){
			message = 0;
		}
		if(obj.getFieldName()==null || obj.getFieldName().trim().length()<=0){
			message = 0;
		}
		if(obj.getKeyName()==null || obj.getKeyName().trim().length()<=0){
			message = 0;
		}
		if(obj.getKeyValue()==null || obj.getKeyValue().trim().length()<=0){
			message = 0;
		}
		if(message ==1){
			cmsBaseSourceService.updateFieldValue(obj);
		}
		HashMap<String, String> model = new HashMap<String, String>();
		model.put("message", String.valueOf(message));
		return new ModelAndView(BaseUrl.message, model);
    }

}
